function [y_predict,MSE] = KNN5(train_data,train_labels,test_data,test_labels,Default)
%五分法KNN

%制作标签
train_labels(train_labels >= 0.4) = 0.5;
train_labels(train_labels <= -0.4) = -0.5;
train_labels(train_labels > 0 & train_labels < 0.4) = 0.2;
train_labels(train_labels > -0.4 & train_labels < 0) = -0.2;

numbers_5 = length(find(train_labels==0.5));
numbers_negative5 = length(find(train_labels==-0.5));
numbers_2 = length(find(train_labels==0.2));
numbers_negative2 = length(find(train_labels==-0.2));
numbers_0 = length(find(train_labels==0));
numbers=min([numbers_0,numbers_2,numbers_5,numbers_negative2,numbers_negative5]);

numbers_5=numbers;
numbers_negative5 = numbers;
numbers_2 = numbers;
numbers_negative2 = numbers;
numbers_0 = numbers;

train_labels_new=[];
train_data_new = [];
j=1;
i=1;

%设计出一个五等份的数据集
while(i<=5*numbers)
    if (train_labels(j)==0.5 &&numbers_5>0)
        train_labels_new(i) = train_labels(j);
        train_data_new(i,:) = train_data(j,:);
        j=j+1;
        i=i+1;
        numbers_5 = numbers_5-1;
    elseif (train_labels(j)==0.2 &&numbers_2>0)
        train_labels_new(i) = train_labels(j);
        train_data_new(i,:) = train_data(j,:);
        j=j+1;
        i=i+1;
        numbers_2 = numbers_2-1;
    elseif (train_labels(j)==0 &&numbers_0>0)
        train_labels_new(i) = train_labels(j);
        train_data_new(i,:) = train_data(j,:);
        j=j+1;
        i=i+1;
        numbers_0 = numbers_0-1;
    elseif (train_labels(j)==-0.2 &&numbers_negative2>0)
        train_labels_new(i) = train_labels(j);
        train_data_new(i,:) = train_data(j,:);
        j=j+1;
        i=i+1;
        numbers_negative2 = numbers_negative2-1;
    elseif (train_labels(j)==-0.5 &&numbers_negative5>0)
        train_labels_new(i) = train_labels(j);
        train_data_new(i,:) = train_data(j,:);
        j=j+1;
        i=i+1;
        numbers_negative5 = numbers_negative5-1;
    else
        j=j+1;
    end
end

test_labels(test_labels >= 0.4) = 0.5;
test_labels(test_labels <= -0.4) = -0.5;
test_labels(test_labels > 0 & test_labels < 0.4) = 0.2;
test_labels(test_labels > -0.4 & test_labels < 0) = -0.2;

%初始化数组
predictions=[];
labels=[];

%训练模型
if Default
    tic
    load("KNN5Model.mat");
    toc
else
    %参数范围
    %{randomsearch,bayesopt,gridsearch}
    %部分优化结果
    %train_data_new,train_labels_new
    BestModel = fitcknn(train_data, train_labels, ...
        'Standardize', true, ...
        'HyperparameterOptimizationOptions',struct(...
        'Optimizer','bayesopt',...
        'KFold',5,'MaxObjectiveEvaluations',200,...
        'NumGridDivisions',20),...
        'OptimizeHyperparameters','auto');
    save("KNN5Model","BestModel");
end

y_predict = predict(BestModel,test_data);
MSE = mse(y_predict,test_labels);
error =zeros(length(test_data),1);
index = find(abs(y_predict-test_labels)>0.2);
error(index) = 1;
index = find(y_predict.*test_labels<0);
error(index) = 1;
output = table(test_data,test_labels,y_predict,error);
filename = 'KNN5.xlsx';
writetable(output,filename,'Sheet',1,'Range','A1')
end